*********************'
* DYNAMIC ANALYSIS *
********************

clear all
clear matrix
set more off
set mat 10000
cd "E:/ProjData/Workplace, elite/"
capture log close
log using "C:/Userdata/Shared/Logs/Workplace, elite/event.text", replace text									
do "C:/Userdata/Shared/Dofiles/DoData/Workplace, elite/programs.do"		


use "finaldata.dta", clear

********************************************************************************
* Basic set-ups ****************************************************************
********************************************************************************

global covs "yearseducation zink"												
global wpssykcovslag "wpssyk_zink_lag wpssyk_yearseducation_lag wpssyk_gender_lag wpssyk_immig_lag"
global esttabbasic "replace label se nogaps b(5) compress booktabs nonotes star(* 0.1 ** 0.05 *** 0.01) varlabels(_cons "Constant") eqlabels(none)"
global depvar "i_nominated"
global indvar "maintreatvar"
global restriction "i_nominated_lag != 1 & i_nominated_lag2 != 1 & i_nominated_lag3 != 1 & i_nominated_lag4 != 1 & i_nominated_lag5 != 1" 	/*First time someone becomes a politician*/					

egen identificationvar = group(LopNr wp occupation) 							/* Main identification variable */
egen wpoccupation = group(wp occupation)


********************************************************************************
* Event study 
********************************************************************************

*Only those who remain at the same place all periods (simplicity)
bysort LopNr: egen maxwp = mean(identificationvar)
keep if maxwp==identificationvar
bysort LopNr: gen N = _N
drop maxwp

*Period after period; no jumps
keep if N>1

sort LopNr mandatperiod
bysort LopNr: gen m_period = mandatperiod[_n+1] 
gen m = m_period-mandatperiod
bysort LopNr: egen m_max = max(m)
keep if m_max==1
drop m_period m m_max

*14 million obs
tab N

*Identify workplaces that had a change; an additional person becomes 
*a politician 
sort LopNr mandatperiod
bysort LopNr: gen dropvar = (s_wpssyk_nominated > 0 & s_wpssyk_nominated[_n-1] == 0 & s_wpssyk_nominated != .)
bysort LopNr: egen mean_dropvar = mean(dropvar)
gen wpssyk_dum = 1 if s_wpssyk_nominated > 0
bysort LopNr: egen mean_wpssyk_nom = mean(wpssyk_dum)

*Drop all who had politicians all periods:
*drop if mean_dropvar==0 & mean_wpssyk_nom==1
*drop dropvar mean_dropvar wpssyk_dum mean_wpssyk_nom

*13 million obs

sort LopNr mandatperiod
*Take out first period something happens
bysort LopNr: gen mandatperiodtreat = mandatperiod if s_wpssyk_nominated > 0 & s_wpssyk_nominated != .
*bysort wpoccupation: egen mandatperiodtreatmin = min(mandatperiodtreat)
bysort LopNr: egen mandatperiodtreat_min = min(mandatperiodtreat)
*drop if mandatperiodtreat_min==1

**Try and drop those who have added pol from work
*if the increase from one period to the next is the same 
* put a 1 for the workplace and drop that ind. 

*Do this for 2-3, 3-4, 4-5, 5-6
forvalues i = 1(1)5 {
preserve
drop if mandatperiodtreat_min==. 
keep if mandatperiod== `i' | mandatperiod==`i'+1
bysort LopNr: gen n = _N
keep if n==2
collapse (sum) i_nominated (max) s_wpssyk_nominated , by(wpoccupation mandatperiod)

*Add up to if a workplace and mandatperiod where only internal nominees
sort wpoccupation mandatperiod
bysort wpoccupation: gen i_nom_f = i_nominated[_n+1] 
gen diff_nom = i_nom_f-i_nominated
sort wpoccupation mandatperiod
bysort wpoccupation: gen pol_f = s_wpssyk_nominated[_n+1] 
gen diff_pol = pol_f-s_wpssyk_nominated
gen internal = 1 if diff_nom==diff_pol & diff_nom>0 & diff_nom!=.
sort wpoccupation mandatperiod
bysort wpoccupation: gen internal_nom = internal[_n-1] 
keep wpoccupation mandatperiod internal_nom
keep if mandatperiod==`i'+1

save "eventdata_perid_`i'.dta", replace
restore

}

preserve 
clear
use "eventdata_perid_1.dta"
forvalues i = 2(1)5 {
append using "eventdata_perid_`i'.dta"
save "eventdata_perid_all.dta", replace
}
restore

*add to other data and drop those whose minimum mandateoriod 
*matches one where there were only internal nominees. 

*?

*Event variables
gen eventtid =  mandatperiod - mandatperiodtreat_min

tab eventtid, gen(y)

*Never treated
foreach i of numlist 1(1)11 {
replace y`i' = 0 if  y`i' ==.
} 

sort LopNr mandatperiod
bysort LopNr: gen i_nom = i_nominated[_n+1] 


save "eventdata_ind.dta", replace


*MErge those from internal changeuse 
merge m:1 wpoccupation mandatperiod using "eventdata_perid_all.dta"
gen inter = 1 if eventtid==0 & internal_nom==1
replace inter = 0 if inter==.
sort LopNr mandatperiod
bysort LopNr: egen inter_allper = max(inter) 

*Drop reference category
drop y4
drop if inter_allper==1

*Difference
sort LopNr mandatperiod
bysort LopNr: gen i_nomlag = i_nominated[_n-1] 
gen nom_diff = i_nominated-i_nomlag
gen nom_diff1 = i_nom-i_nominated

preserve

reghdfe nom_diff y1 y2 y3 y5 y6 y7 y8 y9 y10 y11 if N_wpssyk <= 10 & N_wpssyk >= 4, absorb(LopNr mandatperiod##Kkod ) 

matrix A = J(15, 4,.)

foreach i in 2 3 5 6 7 8 9 10 {

matrix A[`i',1] = _b[y`i']
matrix A[`i',2] = _b[y`i']+1.96*_se[y`i']
matrix A[`i',3] = _b[y`i']-1.96*_se[y`i']
matrix A[`i',4] = `i'-6

}
svmat A
keep A1 A2 A3 A4

gen n = _n
replace A1 =0 if n==4
replace A2=0 if n==4
replace A3=0 if n==4
replace A4 =-2 if n==4 

graph twoway rcap A2 A3 A4, lstyle(ci) || scatter A1 A4, mstyle(p1) ///
graphregion(color(white)) xline(0, lpattern(dash) lcolor(black))  xlabel(-4(1)4) yline(0, lcolor(red)) ytitle(Coefficient size) xtitle("Number of mandate periods before switch") legend(order(1 "95 % Confidence Interval" 2 "Coefficient"))
graph export "C:\Userdata\Shared\Output\Workplace, elite\event_ind410.pdf", replace

restore

*Roths pre trend test; heavily undrpowered test
*reghdfe nom_diff y1 y2 y3 y5 y6 y7 y8 y9 y10 y11 if N_wpssyk <= 10 & N_wpssyk >= 4, absorb(LopNr mandatperiod##Kkod ) 
*pretrends power 0.5, pre(1/3) post(4/10)





preserve

reghdfe nom_diff y1 y2 y3 y5 y6 y7 y8 y9 y10 y11 if N_wpssyk >10 & N_wpssyk <= 30 , absorb(mandatperiod##Kkod LopNr)  

matrix A = J(15, 4,.)

foreach i in 2 3 5 6 7 8 9 10 {

matrix A[`i',1] = _b[y`i']
matrix A[`i',2] = _b[y`i']+1.96*_se[y`i']
matrix A[`i',3] = _b[y`i']-1.96*_se[y`i']
matrix A[`i',4] = `i'-6

}
svmat A
keep A1 A2 A3 A4

gen n = _n
replace A1 =0 if n==4
replace A2=0 if n==4
replace A3=0 if n==4
replace A4 =-2 if n==4 

graph twoway rcap A2 A3 A4, lstyle(ci) || scatter A1 A4, mstyle(p1) ///
graphregion(color(white)) xline(0, lpattern(dash) lcolor(black))  xlabel(-4(1)4) yline(0, lcolor(red)) ytitle(Coefficient size) xtitle("Number of mandate periods before switch") legend(order(1 "95 % Confidence Interval" 2 "Coefficient"))
graph export "C:\Userdata\Shared\Output\Workplace, elite\event_ind1030.pdf", replace

restore



*Event study Sun och Abraham

ssc install eventstudyinteract

*drop if mandatperiodtreat_min==1

gen time_event = mandatperiod if eventtid==0
bysort LopNr: egen firstevent = max(time_event)

*replace firstevent=. if firstevent==6

gen last_treated =0
replace last_treated=1 if firstevent==6

gen never_treated = 0 
replace never_treated=1 if firstevent==.
replace never_treated=1 if last_treated==1

*leads and lags
gen L0 = y6
gen L1 = y7
gen L2 = y8 
gen L3 = y9
gen L4 = y10

gen F1 = y5
*gen F2 = y4
gen F3 = y3
gen F4 = y2


eventstudyinteract nom_diff y1 y2 y3 y5 y6 y7 y8 y9 y10 y11 if N_wpssyk <= 10 & N_wpssyk>=4, absorb(LopNr mandatperiod##Kkod) cohort(firstevent) control_cohort(never_treated)
*help event_plot
*event_plot e(b_iw)#e(V_iw), default_look graph_opt(xtitle("Periods since the event") ytitle("Average effect") xlabel(-10(1)10) ///
		*title("eventstudyinteract")) stub_lag(L#) stub_lead(F#) together


*Create a matrix woth weighted coefficents 		
matrix C = e(b_iw)
mata st_matrix("A",sqrt(diagonal(st_matrix("e(V_iw)"))))
matrix C = C \ A'
matrix list C


*Need to manyally change it beacause we dont need the two outer ones 
*and we lack the control group t-1

matrix D = J(2, 9, .)
matrix D[1,1] = C[1,2]
matrix D[1,2] = C[1,3]

foreach y of numlist 4(1)9 {
matrix D[1,`y'] = C[1,`y']
}

matrix D[2,1] = C[2,2]
matrix D[2,2] = C[2,3]

foreach y of numlist 4(1)9 {
matrix D[2,`y'] = C[2,`y']
}

matrix D[1,3] = 0
matrix D[2,3] = 0


matrix list D

*The plot:
coefplot matrix(D[1]), se(D[2]) vertical yline(0) xline(5, lpattern(dash)) xlabel(1 "-4" 2 "-3" 3 "-2" 4 "-1" 5 "0" 6 "1" 7 "2" 8 "3" 9 "4") xtitle("Number of mandate periods before switch") ytitle("Coefficients") graphregion(color(white))

graph export "C:\Userdata\Shared\Output\Workplace, elite\event_sabr.pdf", replace

	

eventstudyinteract nom_diff y1 y2 y3 y5 y6 y7 y8 y9 y10 y11 if N_wpssyk > 10 & N_wpssyk<=30, absorb(LopNr mandatperiod##Kkod) cohort(firstevent) control_cohort(never_treated)
*help event_plot
*event_plot e(b_iw)#e(V_iw), default_look graph_opt(xtitle("Periods since the event") ytitle("Average effect") xlabel(-10(1)10) ///
		*title("eventstudyinteract")) stub_lag(L#) stub_lead(F#) together

		
matrix C = e(b_iw)
mata st_matrix("A",sqrt(diagonal(st_matrix("e(V_iw)"))))
matrix C = C \ A'
matrix list C


*Need to manyally change it beacause we dont need the two outer ones 
*and we lack the control group t-1

matrix D = J(2, 9, .)
matrix D[1,1] = C[1,2]
matrix D[1,2] = C[1,3]

foreach y of numlist 4(1)9 {
matrix D[1,`y'] = C[1,`y']
}

matrix D[2,1] = C[2,2]
matrix D[2,2] = C[2,3]

foreach y of numlist 4(1)9 {
matrix D[2,`y'] = C[2,`y']
}

matrix D[1,3] = 0
matrix D[2,3] = 0


matrix list D


coefplot matrix(D[1]), se(D[2]) vertical yline(0) xline(5, lpattern(dash)) xlabel(1 "-4" 2 "-3" 3 "-2" 4 "-1" 5 "0" 6 "1" 7 "2" 8 "3" 9 "4") xtitle("Number of mandate periods before switch") ytitle("Coefficients") graphregion(color(white))

graph export "C:\Userdata\Shared\Output\Workplace, elite\event_sabr_2.pdf", replace

clear all
clear matrix
capture log close


